package com.example.springboot.controller;

import com.example.springboot.entity.*;
import com.example.springboot.service.LaundryOptionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/laundry-options")
public class LaundryOptionController {

    @Autowired
    private LaundryOptionService laundryOptionService;

    // 获取所有洗衣选项
    @GetMapping
    public Map<String, Object> getAllOptions(
            @RequestParam(defaultValue = "1") int packagePage,
            @RequestParam(defaultValue = "1") int waterPage,
            @RequestParam(defaultValue = "1") int washingPage,
            @RequestParam(defaultValue = "1") int scentPage,
            @RequestParam(defaultValue = "1") int dryingPage,
            @RequestParam(defaultValue = "10") int size,
            HttpServletRequest request) {
        String role = request.getHeader("Role");
        String username = request.getHeader("Username");
        return laundryOptionService.getAllOptions(packagePage, waterPage, washingPage, scentPage, dryingPage, size, role, username);
    }

    // 新增套餐
    @PostMapping("/package")
    public Map<String, Object> createPackage(
            @RequestBody LaundryPackage laundryPackage,
            HttpServletRequest request) {
        String role = request.getHeader("Role");
        String username = request.getHeader("Username");
        boolean success = laundryOptionService.createPackage(laundryPackage, role, username);
        Map<String, Object> response = new HashMap<>();
        response.put("success", success);
        response.put("message", success ? "套餐添加成功" : "套餐添加失败");
        return response;
    }

    // 更新套餐
    @PutMapping("/package")
    public Map<String, Object> updatePackage(
            @RequestBody LaundryPackage laundryPackage,
            HttpServletRequest request) {
        String role = request.getHeader("Role");
        String username = request.getHeader("Username");
        boolean success = laundryOptionService.updatePackage(laundryPackage, role, username);
        Map<String, Object> response = new HashMap<>();
        response.put("success", success);
        response.put("message", success ? "套餐更新成功" : "套餐更新失败");
        return response;
    }

    // 删除套餐
    @DeleteMapping("/package/{packageId}")
    public Map<String, Object> deletePackage(
            @PathVariable Long packageId,
            HttpServletRequest request) {
        String role = request.getHeader("Role");
        String username = request.getHeader("Username");
        boolean success = laundryOptionService.deletePackage(packageId, role, username);
        Map<String, Object> response = new HashMap<>();
        response.put("success", success);
        response.put("message", success ? "套餐删除成功" : "套餐删除失败");
        return response;
    }

    // 新增水量选项
    @PostMapping("/water-option")
    public Map<String, Object> createWaterOption(
            @RequestBody WaterAmountOption waterOption,
            HttpServletRequest request) {
        String role = request.getHeader("Role");
        String username = request.getHeader("Username");
        boolean success = laundryOptionService.createWaterOption(waterOption, role, username);
        Map<String, Object> response = new HashMap<>();
        response.put("success", success);
        response.put("message", success ? "水量选项添加成功" : "水量选项添加失败");
        return response;
    }

    // 更新水量选项
    @PutMapping("/water-option")
    public Map<String, Object> updateWaterOption(
            @RequestBody WaterAmountOption waterOption,
            HttpServletRequest request) {
        String role = request.getHeader("Role");
        String username = request.getHeader("Username");
        boolean success = laundryOptionService.updateWaterOption(waterOption, role, username);
        Map<String, Object> response = new HashMap<>();
        response.put("success", success);
        response.put("message", success ? "水量选项更新成功" : "水量选项更新失败");
        return response;
    }

    // 删除水量选项
    @DeleteMapping("/water-option/{waterOptionId}")
    public Map<String, Object> deleteWaterOption(
            @PathVariable Long waterOptionId,
            HttpServletRequest request) {
        String role = request.getHeader("Role");
        String username = request.getHeader("Username");
        boolean success = laundryOptionService.deleteWaterOption(waterOptionId, role, username);
        Map<String, Object> response = new HashMap<>();
        response.put("success", success);
        response.put("message", success ? "水量选项删除成功" : "水量选项删除失败");
        return response;
    }

    // 新增洗涤程序选项
    @PostMapping("/washing-option")
    public Map<String, Object> createWashingOption(
            @RequestBody WashingProgramOption washingOption,
            HttpServletRequest request) {
        String role = request.getHeader("Role");
        String username = request.getHeader("Username");
        boolean success = laundryOptionService.createWashingOption(washingOption, role, username);
        Map<String, Object> response = new HashMap<>();
        response.put("success", success);
        response.put("message", success ? "洗涤程序选项添加成功" : "洗涤程序选项添加失败");
        return response;
    }

    // 更新洗涤程序选项
    @PutMapping("/washing-option")
    public Map<String, Object> updateWashingOption(
            @RequestBody WashingProgramOption washingOption,
            HttpServletRequest request) {
        String role = request.getHeader("Role");
        String username = request.getHeader("Username");
        boolean success = laundryOptionService.updateWashingOption(washingOption, role, username);
        Map<String, Object> response = new HashMap<>();
        response.put("success", success);
        response.put("message", success ? "洗涤程序选项更新成功" : "洗涤程序选项更新失败");
        return response;
    }

    // 删除洗涤程序选项
    @DeleteMapping("/washing-option/{programId}")
    public Map<String, Object> deleteWashingOption(
            @PathVariable Long programId,
            HttpServletRequest request) {
        String role = request.getHeader("Role");
        String username = request.getHeader("Username");
        boolean success = laundryOptionService.deleteWashingOption(programId, role, username);
        Map<String, Object> response = new HashMap<>();
        response.put("success", success);
        response.put("message", success ? "洗涤程序选项删除成功" : "洗涤程序选项删除失败");
        return response;
    }

    // 新增洗涤剂香味选项
    @PostMapping("/scent-option")
    public Map<String, Object> createScentOption(
            @RequestBody DetergentScentOption scentOption,
            HttpServletRequest request) {
        String role = request.getHeader("Role");
        String username = request.getHeader("Username");
        boolean success = laundryOptionService.createScentOption(scentOption, role, username);
        Map<String, Object> response = new HashMap<>();
        response.put("success", success);
        response.put("message", success ? "香味选项添加成功" : "香味选项添加失败");
        return response;
    }

    // 更新洗涤剂香味选项
    @PutMapping("/scent-option")
    public Map<String, Object> updateScentOption(
            @RequestBody DetergentScentOption scentOption,
            HttpServletRequest request) {
        String role = request.getHeader("Role");
        String username = request.getHeader("Username");
        boolean success = laundryOptionService.updateScentOption(scentOption, role, username);
        Map<String, Object> response = new HashMap<>();
        response.put("success", success);
        response.put("message", success ? "香味选项更新成功" : "香味选项更新失败");
        return response;
    }

    // 删除洗涤剂香味选项
    @DeleteMapping("/scent-option/{scentId}")
    public Map<String, Object> deleteScentOption(
            @PathVariable Long scentId,
            HttpServletRequest request) {
        String role = request.getHeader("Role");
        String username = request.getHeader("Username");
        boolean success = laundryOptionService.deleteScentOption(scentId, role, username);
        Map<String, Object> response = new HashMap<>();
        response.put("success", success);
        response.put("message", success ? "香味选项删除成功" : "香味选项删除失败");
        return response;
    }

    // 新增烘干程序选项
    @PostMapping("/drying-option")
    public Map<String, Object> createDryingOption(
            @RequestBody DryingProgramOption dryingOption,
            HttpServletRequest request) {
        String role = request.getHeader("Role");
        String username = request.getHeader("Username");
        boolean success = laundryOptionService.createDryingOption(dryingOption, role, username);
        Map<String, Object> response = new HashMap<>();
        response.put("success", success);
        response.put("message", success ? "烘干选项添加成功" : "烘干选项添加失败");
        return response;
    }

    // 更新烘干程序选项
    @PutMapping("/drying-option")
    public Map<String, Object> updateDryingOption(
            @RequestBody DryingProgramOption dryingOption,
            HttpServletRequest request) {
        String role = request.getHeader("Role");
        String username = request.getHeader("Username");
        boolean success = laundryOptionService.updateDryingOption(dryingOption, role, username);
        Map<String, Object> response = new HashMap<>();
        response.put("success", success);
        response.put("message", success ? "烘干选项更新成功" : "烘干选项更新失败");
        return response;
    }

    // 删除烘干程序选项
    @DeleteMapping("/drying-option/{dryingId}")
    public Map<String, Object> deleteDryingOption(
            @PathVariable Long dryingId,
            HttpServletRequest request) {
        String role = request.getHeader("Role");
        String username = request.getHeader("Username");
        boolean success = laundryOptionService.deleteDryingOption(dryingId, role, username);
        Map<String, Object> response = new HashMap<>();
        response.put("success", success);
        response.put("message", success ? "烘干选项删除成功" : "烘干选项删除失败");
        return response;
    }
}